using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.Odbc;
using System.Collections;
using DTO;

namespace DAO
{
    public class MuonSachDAO
    {
        DataProvider _provider = new DataProvider();
        public MuonSachDAO()
        {
            _provider.connect();
        }

        public ArrayList GetMuonSachDTO(String madocgia)
        {
            ArrayList arr = new ArrayList();
            string query = "select *from DocGia where MaDocGia='" + madocgia + "'";
            OdbcDataAdapter adapter = _provider.OdbcAdapter(query);
            DataSet ds = new DataSet();
            adapter.Fill(ds);
            DataTable dt = ds.Tables[0];
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                MuonSachDTO info = new MuonSachDTO();
                info.ISBN = dt.Rows[i]["ISBN"].ToString();
                info.MaSach = dt.Rows[i]["MaSach"].ToString();
                info.MaDocGia = dt.Rows[i]["MaDocGia"].ToString();
                info.NgayMuon = DateTime.Parse(dt.Rows[i]["NgayMuon"].ToString());
                info.NgayHetHan = DateTime.Parse(dt.Rows[i]["NgayHetHan"].ToString());                
                info.SoLuongMuon = int.Parse(dt.Rows[i]["SoLuongMuon"].ToString());
                info.SoLuongTra = int.Parse(dt.Rows[i]["SoLuongTra"].ToString());
                arr.Add(info);
            }
            return arr;
        }
            
        //ham insert vao bang muonsach voi 2 tham so la isbn,masach va madocgia
        public void Insert(String isbn, String masach, String madocgia)
        {
            DateTime ngaymuon = DateTime.Now;
            long temp = ngaymuon.Ticks;
            int songay = SoNgayMuonToiDa();
            long time = songay * 24 * 3600;
            DateTime ngayhethan = new DateTime(temp + 10000000 * time);
            String insertmuonsach = "Insert into MuonSach values('" + isbn + "','" + masach + "','" + madocgia + "','" + ngaymuon + "','" + ngayhethan + "')";
            _provider.executeNonQuery(insertmuonsach);
            
        }
        public int SoNgayMuonToiDa()
        {
            String ten = "SoNgayMuonToiDa";
            string query = "select * from ThayDoiQuiDinh where Ten = '"+ten+"'";
            OdbcDataAdapter adapter = _provider.OdbcAdapter(query);
            DataSet ds = new DataSet();
            adapter.Fill(ds);
            DataTable dt = ds.Tables[0];
            return int.Parse(dt.Rows[0]["GiaTri"].ToString());
        }

        public DataTable GetTableMuonSach()
        {
            String stringSqlQrery = "Select * from MuonSach";
            OdbcDataAdapter da = _provider.OdbcAdapter(stringSqlQrery);
            DataSet ds = new DataSet();
            da.Fill(ds);
            _provider.disconnect();
            return ds.Tables[0];
        }
    }
}
